/**
 * crawler
 *
 * outlook@China software studio
 * Copyright (c) 2008-2010. All Rights Reserved.
 * http://www.outlook.com
 */

package org.outlook.common.cachelet;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;

/**
 * function:
 * @author Watson email: watson.wu@hotmail.com
 * @date 2011
 */
public class CacheletManager {

	private static HashMap<String, Object> cacheMap = new HashMap<String, Object>();

    private CacheletManager() {
        super();
    }

    public static boolean getSimpleFlag(String key){
        try{
            return (Boolean) cacheMap.get(key);
        }catch(NullPointerException e){
            return false;
        }
    }
    public static long getServerStartdt(String key){
        try {
            return (Long)cacheMap.get(key);
        } catch (Exception ex) {
            return 0;
        }
    }

    public synchronized static boolean setSimpleFlag(String key,boolean flag){
        if (flag && getSimpleFlag(key)) {
            return false;
        }else{
            cacheMap.put(key, flag);
            return true;
        }
    }
    public synchronized static boolean setSimpleFlag(String key,long serverbegrundt){
        if (cacheMap.get(key) == null) {
            cacheMap.put(key,serverbegrundt);
            return true;
        }else{
            return false;
        }
    }


    private synchronized static Cachelet getCache(String key) {
        return (Cachelet) cacheMap.get(key);
    }

    private synchronized static boolean hasCache(String key) {
        return cacheMap.containsKey(key);
    }

    public synchronized static void clearAll() {
        cacheMap.clear();
    }

    public synchronized static void clearAll(String type) {
        Iterator<Entry<String, Object>> i = cacheMap.entrySet().iterator();
        String key;
        ArrayList<String> arr = new ArrayList<String>();
        try {
            while (i.hasNext()) {
                Entry<String, Object> entry = (Entry<String, Object>) i.next();
                key = (String) entry.getKey();
                if (key.startsWith(type)) {
                    arr.add(key);
                }
            }
            for (int k = 0; k < arr.size(); k++) {
                clearOnly(arr.get(k));
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public synchronized static void clearOnly(String key) {
        cacheMap.remove(key);
    }

    public synchronized static void putCache(String key, Cachelet obj) {
        cacheMap.put(key, obj);
    }

    public static Cachelet getCacheInfo(String key) {

        if (hasCache(key)) {
        	Cachelet cache = getCache(key);
            if (cacheExpired(cache)) {
                cache.setExpired(true);
            }
            return cache;
        }else
            return null;
    }

    public static void putCacheInfo(String key, Cachelet obj, long dt,boolean expired) {
    	Cachelet cache = new Cachelet();
        cache.setKey(key);
        cache.setTimeOut(dt + System.currentTimeMillis());
        cache.setValue(obj);
        cache.setExpired(expired);
        cacheMap.put(key, cache);
    }

    public static void putCacheInfo(String key,Cachelet obj,long dt){
    	Cachelet cache = new Cachelet();
        cache.setKey(key);
        cache.setTimeOut(dt+System.currentTimeMillis());
        cache.setValue(obj);
        cache.setExpired(false);
        cacheMap.put(key,cache);
    }

    public static boolean cacheExpired(Cachelet cache) {
        if (null == cache) {
            return false;
        }
        long nowDt = System.currentTimeMillis();
        long cacheDt = cache.getTimeOut();
        if (cacheDt <= 0||cacheDt>nowDt) {
            return false;
        } else {
            return true;
        }
    }

    public static int getCacheSize() {
        return cacheMap.size();
    }

    public static int getCacheSize(String type) {
        int k = 0;
        Iterator<Entry<String, Object>> i = cacheMap.entrySet().iterator();
        String key;
        try {
            while (i.hasNext()) {
                Entry<String, Object> entry = (Entry<String, Object>) i.next();
                key = (String) entry.getKey();
                if (key.indexOf(type) != -1) {
                    k++;
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }

        return k;
    }

    public static ArrayList<String> getCacheAllkey() {
        ArrayList<String> a = new ArrayList<String>();
        try {
            Iterator<Entry<String, Object>> i = cacheMap.entrySet().iterator();
            while (i.hasNext()) {
                Entry<String, Object> entry = (Entry<String, Object>) i.next();
                a.add((String) entry.getKey());
            }
        } catch (Exception ex) {} finally {
        }
        return a;
    }

    public static ArrayList<String> getCacheListkey(String type) {
        ArrayList<String> a = new ArrayList<String>();
        String key;
        try {
            Iterator<Entry<String, Object>> i = cacheMap.entrySet().iterator();
            while (i.hasNext()) {
            	Entry<String, Object> entry = (Entry<String, Object>) i.next();
                key = (String) entry.getKey();
                if (key.indexOf(type) != -1) {
                    a.add(key);
                }
            }
        } catch (Exception ex) {} finally {
        }
        return a;
    }

    public static void main(String[] args) {
    	System.out.println(CacheletManager.getSimpleFlag("alksd"));
        CacheletManager.putCache("abc", new Cachelet());
        CacheletManager.putCache("def", new Cachelet());
        CacheletManager.putCache("ccc", new Cachelet());
        CacheletManager.clearOnly("");
        Cachelet c = new Cachelet();
        for (int i = 0; i < 10; i++) {
            CacheletManager.putCache("" + i, c);
        }
        CacheletManager.putCache("aaaaaaaa", c);
        CacheletManager.putCache("abchcy;alskd", c);
        CacheletManager.putCache("cccccccc", c);
        CacheletManager.putCache("abcoqiwhcy", c);
        System.out.println("size of before deleting："+CacheletManager.getCacheSize());
        CacheletManager.getCacheAllkey();
        CacheletManager.clearAll("aaaa");
        System.out.println("size of after deleting："+CacheletManager.getCacheSize());
        CacheletManager.getCacheAllkey();
    }
}